
/*
	Before using this do-file, please make sure to:
	
		- Change the target directories from globals $pdata and $Results
		
		- $pdata is directed to a folder containing two databases:
			(i) 	survey_response.dta
			(ii)	analysis_sample.dta
			A description of these databases is available in the Read me files
			
		- $Results is directed to a folder containing two other folders:
			(i) 	Figures
			(ii)	Tables
			Please create these two folders. In thse folders is where the 
			results will be stored
			
		- Download the following user-written programs:
			(i) 	rddensity
			(ii)	lpdensity
			(iii)	svmat2
		
*/


clear all
global pdata 	"C:\Users\mpmar\Desktop\PhD\Projects\RA work - Romanian Tracking Policy\Submission to RESTAT\Replication files\Databases\Processed"
global Results	"C:\Users\mpmar\Desktop\PhD\Projects\RA work - Romanian Tracking Policy\Submission to RESTAT\Replication files\FT, main text"

global pdata 	"C:\Users\omx9891\Dropbox\Boxifier\Research\romania_schools\replication\"
global Results	"C:\Users\omx9891\Dropbox\Boxifier\Research\romania_schools\replication\"

global pdata 	"C:\Users\oferm\Dropbox\Boxifier\Research\romania_schools\replication\"
global Results 	"C:\Users\oferm\Dropbox\Boxifier\Research\romania_schools\replication\"


********************************************************************************
********************************************************************************
*************************** FIGURES IN MAIN TEXT *******************************
********************************************************************************
********************************************************************************


********************************************************************************
********************************* FIGURE 1 *************************************
********************************************************************************

	****************************************************************************
	****************************************************************************
	**** Figure 1, Panel A: Density test for target sample = 4,350

	*****************************
	**** Setting up the data ****
	*****************************
	
	use "${pdata}\survey_response.dta", clear
		
		
	**********************
	**** Density Test ****
	**********************
		
	rddensity media_std if id_child==id_child_peer			, plot graph_opt(			///
							title(`"Panel A: Target sample = 4,350"', pos(11) 			///
							size(small) color(gs0)) ytitle("Density", 					///
							size(small)) xtitle(`"Transition score"', 					///
							size(small)) legend(off) graphregion(color(white))			///
							xlabel(-2.5 -1.5 -.5 .5 1.5 2.5) 							///
							saving("${Results}\Figures\Figure1A.gph",replace))			///
							plot_range(-2.5 2.5) hist_range(-2.5 2.5) 					
							
	graph export "${Results}\Figures\Figure1A.png", as(png) replace	
	graph export "${Results}\Figures\Figure1A.eps", as(eps) name("Graph") preview(off) replace



	****************************************************************************
	****************************************************************************
	**** Figure 1, Panel B: Density test for respondents = 2,865

	**********************
	**** Density Test ****
	**********************
	
	rddensity media_std if rsurvey==1	& id_child==id_child_peer, plot graph_opt(			///
							title(`"Panel B: Respondents = 2,865"', pos(11) 				///
							size(small) color(gs0)) ytitle("Density", 						///
							size(small)) xtitle(`"Transition score"', 						///
							size(small)) legend(off) graphregion(color(white))				///
							xlabel(-2.5 -1.5 -.5 .5 1.5 2.5) 								///
							saving("${Results}\Figures\Figure1B.gph", replace))				///
							plot_range(-2.5 2.5) hist_range(-2.5 2.5) 
							
	graph export "${Results}\Figures\Figure1B.png", as(png) replace
	graph export "${Results}\Figures\Figure1B.eps", as(eps) name("Graph") preview(off) replace
	
	
	
	****************************************************************************
	****************************************************************************
	**** Figure 1, Panel C: Responded to survey
	
	*SET GLOBAL PARAMETERS
	global bw "0.5"
	global step ".3"
	global end=3.6		// The value of end should be a multiple of step
	global cutwidthA "0($step)$end"
	global cutwidthB "-$end($step)0"

	*CUT MEDIA VARIABLE
	egen media_catA=cut(media_std) if media_std>0, at($cutwidthA)
	egen media_catB=cut(media_std) if media_std<0, at($cutwidthB)

	*gen media_catA=media_std if media_std>0
	*gen media_catB=media_std if media_std<0

	gen 	media_cat=media_catA
	replace media_cat=media_catB if media_cat==.
	replace media_cat=media_cat+($step/2)
	replace media_cat=.15 if media_std==0

	*replace media_cat=2.85 if media_cat>2.84 & media_cat<3.16
	*replace media_cat=-2.55 if media_cat<-2.84 & media_cat>-3.16

	* Make sure we have the same number of points on each side of the threshold
	replace media_cat=2.25 if media_cat>2.24 & media_cat<3.16
	replace media_cat=-2.25 if media_cat<-2.24 & media_cat>-3.16

	* Discontinuity fixed effects
	encode id_discont, gen(id_discontinuity)

	 
	*GENERATE TRENDS
	*gen media_std2=media_std*media_std
	*gen media_std3=media_std*media_std2
	global trend "media_std media_std2 media_std3"
	global trend "media_std media_std2"
	global trend "media_std i.id_discontinuity"
	*global trend "media_std"


	egen media_check = mean(media_std), by(media_cat)

	*REGRESS FOR PREDICTED VALUES - Top vs Bottom

	gen i1 = 1 if media_cat>0
	gen i2 = 1 if media_cat<0
		
			gen rsurvey_s2 = rsurvey if i2==1 & IDC==1
			reg rsurvey $trend if media_std<0 & i2==1 & IDC==1, robust
			predict rsurvey_A2 if e(sample)
			predict rsurvey_A_se2 if e(sample), stdp
			gen rsurvey_A_low2 = rsurvey_A2 - 1.96*rsurvey_A_se2
			gen rsurvey_A_high2 = rsurvey_A2 + 1.96*rsurvey_A_se2
			
			gen rsurvey_s1 = rsurvey if i1==1 & IDC==1
			reg rsurvey $trend if media_std>0 & i1==1 & IDC==1, robust
			predict rsurvey_B1 if e(sample)
			predict rsurvey_B_se1 if e(sample), stdp
			gen rsurvey_low1 = rsurvey_B1 - 1.96*rsurvey_B_se1
			gen rsurvey_high1 = rsurvey_B1 + 1.96*rsurvey_B_se1

	keep if IDC==1
	collapse rsurvey* any_response1* any_response2* any_response3* any_response4* any_response5* 	///
				all_response1* all_response2* all_response3* all_response4* all_response5* IDC		///
				(count) i1 i2, by(media_cat)
		

	replace media_cat =. if media_cat ==0

	************************************
	**** Figure, responds to survey ****
	************************************
	
	#delimit;

	twoway 	(scatter rsurvey_s1 media_cat if IDC==1 & media_cat>0, msymbol(Oh) msize(small) mcolor(gs10) sort) 		|| 
			(lfit rsurvey_B1 media_cat if IDC==1 & media_cat>0, clcolor(gs0) clwidth(thick) clpattern(solid))		||
			(scatter rsurvey_s2 media_cat if IDC==1 & media_cat<0, msymbol(Oh) msize(small) mcolor(gs10) sort) 		|| 
			(lfit rsurvey_A2 media_cat if IDC==1 & media_cat<0, clcolor(gs0) clwidth(thick) clpattern(solid)), 
			 legend(off)
			 xlabel(-2.5(1)2.5)
			 ylabel(0(.2)1)
			 title(`"Panel C: Responded to survey"', position(11) size(small))
			 ytitle(Response rate, size(small))
			 xtitle(`"Normed transition score"', size(small))
			 xline(0)
			 scheme(s1color)
			 saving("${Results}\Figures\Figure1C.gph",replace);
	
	#delimit cr	
	
	graph export "${Results}\Figures\Figure1C.png", as(png) replace	
	graph export "${Results}\Figures\Figure1C.eps", as(eps) name("Graph") preview(off) replace
			 
			 
	****************************************************************************
	****************************************************************************
	**** Figure 1, Panel D: Likelihood of being in the top class

	*****************************
	**** Setting up the data ****
	*****************************
	
	use "$pdata\analysis_sample.dta", clear

	*SET GLOBAL PARAMETERS
	global bw "0.5"
	global step ".3"
	global end=3.6		// The value of end should be a multiple of step
	global cutwidthA "0($step)$end"
	global cutwidthB "-$end($step)0"

	*CUT MEDIA VARIABLE
	egen media_catA=cut(media_std) if media_std>0, at($cutwidthA)
	egen media_catB=cut(media_std) if media_std<0, at($cutwidthB)

	*gen media_catA=media_std if media_std>0
	*gen media_catB=media_std if media_std<0

	gen 	media_cat=media_catA
	replace media_cat=media_catB if media_cat==.
	replace media_cat=media_cat+($step/2)
	replace media_cat=.15 if media_std==0

	*replace media_cat=2.85 if media_cat>2.84 & media_cat<3.16
	*replace media_cat=-2.55 if media_cat<-2.84 & media_cat>-3.16

	* Make sure we have the same number of points on each side of the threshold
	replace media_cat=2.25 if media_cat>2.24 & media_cat<3.16
	replace media_cat=-2.25 if media_cat<-2.24 & media_cat>-3.16

	*GENERATE TRENDS
	*gen media_std2=media_std*media_std
	*gen media_std3=media_std*media_std2
	global trend "media_std media_std2 media_std3"
	global trend "media_std media_std2"
	global trend "media_std"


	*REGRESS FOR PREDICTED VALUES - All informants

		gen Bbetter_class=better_class if Bs_disrupt==1 & Bs_confid==1 & 			///
									Bs_effort==1 & Bs_ability==1 & Bs_perform==1

	local i=1

	foreach info in i1 i2 i3 i4 i5 i6{
		
		cap reg better_class $trend if media_std<0 & `info'==1, robust
		cap predict better_class_A`i' if e(sample)
		cap predict better_class_A_se`i' if e(sample), stdp
		cap gen better_class_A_low`i' = better_class_A`i' - 1.96*better_class_A_se`i'
		cap gen better_class_A_high`i' = better_class_A`i' + 1.96*better_class_A_se`i'
		
		cap reg better_class $trend if media_std>0 & `info'==1, robust
		cap predict better_class_B`i' if e(sample)
		cap predict better_class_B_se`i' if e(sample), stdp
		cap gen better_class_B_low`i' = better_class_B`i' - 1.96*better_class_B_se`i'
		cap gen better_class_B_high`i' = better_class_B`i' + 1.96*better_class_B_se`i'

		gen s_disrupt`i'= s_disrupt if `info'==1
		cap reg s_disrupt $trend if media_std<0 & `info'==1, robust
		cap predict s_disrupt_A`i' if e(sample)
		cap predict s_disrupt_A_se`i' if e(sample), stdp
		cap gen s_disrupt_A_low`i' = s_disrupt_A`i' - 1.96*s_disrupt_A_se`i'
		cap gen s_disrupt_A_high`i' = s_disrupt_A`i' + 1.96*s_disrupt_A_se`i'
		
		cap reg s_disrupt $trend if media_std>0 & `info'==1, robust
		cap predict s_disrupt_B`i' if e(sample)
		cap predict s_disrupt_B_se`i' if e(sample), stdp
		cap gen s_disrupt_B_low`i' = s_disrupt_B`i' - 1.96*s_disrupt_B_se`i'
		cap gen s_disrupt_B_high`i' = s_disrupt_B`i' + 1.96*s_disrupt_B_se`i'
		
		gen s_confid`i'= s_confid if `info'==1
		cap reg s_confid $trend if media_std<0 & `info'==1, robust
		cap predict s_confid_A`i' if e(sample)
		cap predict s_confid_A_se`i' if e(sample), stdp
		cap gen s_confid_A_low`i' = s_confid_A`i' - 1.96*s_confid_A_se`i'
		cap gen s_confid_A_high`i' = s_confid_A`i' + 1.96*s_confid_A_se`i'
		
		cap reg s_confid $trend if media_std>0 & `info'==1, robust
		cap predict s_confid_B`i' if e(sample)
		cap predict s_confid_B_se`i' if e(sample), stdp
		cap gen s_confid_B_low`i' = s_confid_B`i' - 1.96*s_confid_B_se`i'
		cap gen s_confid_B_high`i' = s_confid_B`i' + 1.96*s_confid_B_se`i'
		
		gen s_effort`i'= s_effort if `info'==1
		cap reg s_effort $trend if media_std<0 & `info'==1, robust
		cap predict s_effort_A`i' if e(sample)
		cap predict s_effort_A_se`i' if e(sample), stdp
		cap gen s_effort_A_low`i' = s_effort_A`i' - 1.96*s_effort_A_se`i'
		cap gen s_effort_A_high`i' = s_effort_A`i' + 1.96*s_effort_A_se`i'
		
		cap reg s_effort $trend if media_std>0 & `info'==1, robust
		cap predict s_effort_B`i' if e(sample)
		cap predict s_effort_B_se`i' if e(sample), stdp
		cap gen s_effort_B_low`i' = s_effort_B`i' - 1.96*s_effort_B_se`i'
		cap gen s_effort_B_high`i' = s_effort_B`i' + 1.96*s_effort_B_se`i'

		gen s_ability`i'= s_ability if `info'==1
		cap reg s_ability $trend if media_std<0 & `info'==1, robust
		cap predict s_ability_A`i' if e(sample)
		cap predict s_ability_A_se`i' if e(sample), stdp
		cap gen s_ability_A_low`i' = s_ability_A`i' - 1.96*s_ability_A_se`i'
		cap gen s_ability_A_high`i' = s_ability_A`i' + 1.96*s_ability_A_se`i'
		
		cap reg s_ability $trend if media_std>0 & `info'==1, robust
		cap predict s_ability_B`i' if e(sample)
		cap predict s_ability_B_se`i' if e(sample), stdp
		cap gen s_ability_B_low`i' = s_ability_B`i' - 1.96*s_ability_B_se`i'
		cap gen s_ability_B_high`i' = s_ability_B`i' + 1.96*s_ability_B_se`i'

		gen s_perform`i'= s_perform if `info'==1
		cap reg s_perform $trend if media_std<0 & `info'==1, robust
		cap predict s_perform_A`i' if e(sample)
		cap predict s_perform_A_se`i' if e(sample), stdp
		cap gen s_perform_A_low`i' = s_perform_A`i' - 1.96*s_perform_A_se`i'
		cap gen s_perform_A_high`i' = s_perform_A`i' + 1.96*s_perform_A_se`i'
		
		cap reg s_perform $trend if media_std>0 & `info'==1, robust
		cap predict s_perform_B`i' if e(sample)
		cap predict s_perform_B_se`i' if e(sample), stdp
		cap gen s_perform_B_low`i' = s_perform_B`i' - 1.96*s_perform_B_se`i'
		cap gen s_perform_B_high`i' = s_perform_B`i' + 1.96*s_perform_B_se`i'
		
		gen bac_rank`i'= bac_rank if `info'==1 
		cap reg bac_rank $trend if media_std<0 & `info'==1, robust
		cap predict bac_rank_A`i' if e(sample)
		cap predict bac_rank_A_se`i' if e(sample), stdp
		cap gen bac_rank_A_low`i' = bac_rank_A`i' - 1.96*bac_rank_A_se`i'
		cap gen bac_rank_A_high`i' = bac_rank_A`i' + 1.96*bac_rank_A_se`i'
		
		cap reg bac_rank $trend if media_std>0 & `info'==1, robust
		cap predict bac_rank_B`i' if e(sample)
		cap predict bac_rank_B_se`i' if e(sample), stdp
		cap gen bac_rank_B_low`i' = bac_rank_B`i' - 1.96*bac_rank_B_se`i'
		cap gen bac_rank_B_high`i' = bac_rank_B`i' + 1.96*bac_rank_B_se`i'
		
		*gen bac_ranku`i'= bac_ranku if `info'==1
		*cap reg bac_ranku $trend if media_std<0 & `info'==1, robust
		*cap predict bac_ranku_A`i' if e(sample)
		*cap predict bac_ranku_A_se`i' if e(sample), stdp
		*cap gen bac_ranku_A_low`i' = bac_ranku_A`i' - 1.96*bac_ranku_A_se`i'
		*cap gen bac_ranku_A_high`i' = bac_ranku_A`i' + 1.96*bac_ranku_A_se`i'

		*cap reg bac_ranku $trend if media_std>0 & `info'==1, robust
		*cap predict bac_ranku_B`i' if e(sample)
		*cap predict bac_ranku_B_se`i' if e(sample), stdp
		*cap gen bac_ranku_B_low`i' = bac_ranku_B`i' - 1.96*bac_ranku_B_se`i'
		*cap gen bac_ranku_B_high`i' = bac_ranku_B`i' + 1.96*bac_ranku_B_se`i'
		

	local i = `i'+1	

	}

	collapse Bbetter_class better_class* s_disrupt* s_confid* s_effort* s_ability* s_perform* bac_rank*	///
				(count) i1 i2 i3 i4 i5 i6, by(media_cat)

	replace media_cat =. if media_cat ==0

	******************************************************
	**** Figure, likelihood of being in the top class ****
	******************************************************
	
	#delimit;

		scatter better_class media_cat, 
		 msymbol(Oh Oh)
		 msize(small small)
		 mcolor(black black)
		 sort
		 legend(off)
		 xlabel(-2.5(1)2.5)
		 ylabel(0(.25)1)
		 title(`"Panel D: Likelihood of being in the top class"', position(11) size(small))
		 ytitle(Proportion of students, size(small))
		 xtitle(`"Normed transition score"', size(small))
		 xline(0)
		 scheme(s1color)
		 saving("${Results}\Figures\Figure1D.gph",replace);

	#delimit cr	
	
	graph export "${Results}\Figures\Figure1D.png", as(png) replace	
	graph export "${Results}\Figures\Figure1D.eps", as(eps) name("Graph") preview(off) replace
 	

	
	****************************************************************************
	****************************************************************************
	**** Figure 1: Combining Panels A to D
	
	*title("Figure 1: Density checks, response rate, and compliance", pos(6) size(small)) 		///
	
	graph combine  	"${Results}\Figures\Figure1A.gph" "${Results}\Figures\Figure1B.gph" 						///
					"${Results}\Figures\Figure1C.gph" "${Results}\Figures\Figure1D.gph", 						///
					ysize(8) holes(3 6 9) scheme(s1color) graphregion(c(white) margin(2 2 25 25))  				///
					saving("${Results}\Figures\Figure1_AllPanels.gph",replace)
				

				
********************************************************************************
********************************* FIGURE 2 *************************************
********************************************************************************

	****************************************************************
	**** Set up of the data is the same as in Figure 1, Panel D ****
	****************************************************************
	
	*** Student confidence, self-perception
	#delimit;

	twoway 	(scatter s_confid4 media_cat, msymbol(Oh) msize(small) mcolor(blue) sort) 			|| 
			(line s_confid_A4 media_cat, clcolor(blue) clwidth(thick) clpattern(solid))			||
			(line s_confid_B4 media_cat, clcolor(blue) clwidth(thick) clpattern(solid))			||
			(scatter s_confid5 media_cat, msymbol(Oh) msize(small) mcolor(gs4) sort) 			|| 
			(line s_confid_A5 media_cat, clcolor(gs4) clwidth(thin) clpattern(solid))			||
			(line s_confid_B5 media_cat, clcolor(gs4) clwidth(thin) clpattern(solid)), 
			 legend(off)
			 xlabel(-2.5(1)2.5, labsize(medium))
			 ylabel(2(2)10, labsize(medium))
			 title(Panel A: Self-confidence, position(11) size(medlarge))
			 ytitle(Average rank, size(medsmall))
			 xtitle("")
			 xline(0)
			 scheme(s1color)
			 saving("${Results}\Figures\Figure2A.gph",replace);
	
	graph export "${Results}\Figures\Figure2A.png", as(png) replace;
	graph export "${Results}\Figures\Figure2A.eps", as(eps) name("Graph") preview(off) replace;

	
	*** Student confidence, peer reports
	#delimit;
	
	twoway 	(scatter s_confid2 media_cat, msymbol(Oh) msize(small) mcolor(green) sort) 			|| 
			(line s_confid_A2 media_cat, clcolor(green) clwidth(thick) clpattern(shortdash))	||
			(line s_confid_B2 media_cat, clcolor(green) clwidth(thick) clpattern(shortdash))	||
			(scatter s_confid3 media_cat, msymbol(Oh) msize(small) mcolor(red) sort) 			|| 
			(line s_confid_A3 media_cat, clcolor(red) clwidth(thick) clpattern(longdash))		||
			(line s_confid_B3 media_cat, clcolor(red) clwidth(thick) clpattern(longdash))		||
			(scatter s_confid5 media_cat, msymbol(Oh) msize(small) mcolor(gs4) sort) 			|| 
			(line s_confid_A5 media_cat, clcolor(gs4) clwidth(thin) clpattern(solid))			||
			(line s_confid_B5 media_cat, clcolor(gs4) clwidth(thin) clpattern(solid)), 
			 legend(off)
			 xlabel(-2.5(1)2.5, labsize(medium))
			 ylabel(2(2)10, labsize(medium))
			 title(Panel B: Self-confidence, position(11) size(medlarge))
			 ytitle("")
			 xtitle("")
			 xline(0)
			 scheme(s1color)
			 saving("${Results}\Figures\Figure2B.gph",replace);
		 
	graph export "${Results}\Figures\Figure2B.png", as(png) replace;
	graph export "${Results}\Figures\Figure2B.eps", as(eps) name("Graph") preview(off) replace;

	
	*** Academic effort, self-perception
	#delimit;
	
	twoway 	(scatter s_effort4 media_cat, msymbol(Oh) msize(small) mcolor(blue) sort) 			|| 
			(line s_effort_A4 media_cat, clcolor(blue) clwidth(thick) clpattern(solid))			||
			(line s_effort_B4 media_cat, clcolor(blue) clwidth(thick) clpattern(solid))			||
			(scatter s_effort5 media_cat, msymbol(Oh) msize(small) mcolor(gs4) sort) 			|| 
			(line s_effort_A5 media_cat, clcolor(gs4) clwidth(thin) clpattern(solid))			||
			(line s_effort_B5 media_cat, clcolor(gs4) clwidth(thin) clpattern(solid)), 
			 legend(off)
			 xlabel(-2.5(1)2.5, labsize(medium))
			 ylabel(2(2)10, labsize(medlarge))
			 title(Panel C: Academic effort, position(11) size(medlarge))
			 ytitle(Average rank, size(medsmall))
			 xtitle("")
			 xline(0)
			 scheme(s1color)
			 saving("${Results}\Figures\Figure2C.gph",replace);		 
 
	graph export "${Results}\Figures\Figure2C.png", as(png) replace;
	graph export "${Results}\Figures\Figure2C.eps", as(eps) name("Graph") preview(off) replace;
		 
		 
	*** Academic effort, peer reports
	#delimit;
	
	twoway 	(scatter s_effort2 media_cat, msymbol(Oh) msize(small) mcolor(green) sort) 			|| 
			(line s_effort_A2 media_cat, clcolor(green) clwidth(thick) clpattern(shortdash))	||
			(line s_effort_B2 media_cat, clcolor(green) clwidth(thick) clpattern(shortdash))	||
			(scatter s_effort3 media_cat, msymbol(Oh) msize(small) mcolor(red) sort) 			|| 
			(line s_effort_A3 media_cat, clcolor(red) clwidth(thick) clpattern(longdash))		||
			(line s_effort_B3 media_cat, clcolor(red) clwidth(thick) clpattern(longdash))		||
			(scatter s_effort5 media_cat, msymbol(Oh) msize(small) mcolor(gs4) sort) 			|| 
			(line s_effort_A5 media_cat, clcolor(gs4) clwidth(thin) clpattern(solid))			||
			(line s_effort_B5 media_cat, clcolor(gs4) clwidth(thin) clpattern(solid)), 
			 legend(off)
			 xlabel(-2.5(1)2.5, labsize(medium))
			 ylabel(2(2)10, labsize(medium))
			 title(Panel D: Academic effort, position(11) size(medlarge))
			 ytitle("")
			 xtitle("")
			 xline(0)
			 scheme(s1color)
			 saving("${Results}\Figures\Figure2D.gph",replace);		 
			 
		graph export "${Results}\Figures\Figure2D.png", as(png) replace;
		graph export "${Results}\Figures\Figure2D.eps", as(eps) name("Graph") preview(off) replace;
	
	*** Student ability, self-perception
	#delimit;
	
	twoway 	(scatter s_ability4 media_cat, msymbol(Oh) msize(small) mcolor(blue) sort) 			|| 
			(line s_ability_A4 media_cat, clcolor(blue) clwidth(thick) clpattern(solid))		||
			(line s_ability_B4 media_cat, clcolor(blue) clwidth(thick) clpattern(solid))		||
			(scatter s_ability5 media_cat, msymbol(Oh) msize(small) mcolor(gs4) sort) 			|| 
			(line s_ability_A5 media_cat, clcolor(gs4) clwidth(thin) clpattern(solid))			||
			(line s_ability_B5 media_cat, clcolor(gs4) clwidth(thin) clpattern(solid)), 
			 legend(off)
			 xlabel(-2.5(1)2.5, labsize(medium))
			 ylabel(2(2)10, labsize(medium))
			 title(Panel E: Academic ability, position(11) size(medlarge))
			 ytitle(Average rank, size(medsmall))
			 xtitle("")
			 xline(0)
			 scheme(s1color)
			 saving("${Results}\Figures\Figure2E.gph",replace);		 
			 
		graph export "${Results}\Figures\Figure2E.png", as(png) replace;
		graph export "${Results}\Figures\Figure2E.eps", as(eps) name("Graph") preview(off) replace;

	
	*** Student ability, peer reports
	#delimit;
	
	twoway 	(scatter s_ability2 media_cat, msymbol(Oh) msize(small) mcolor(green) sort) 		|| 
			(line s_ability_A2 media_cat, clcolor(green) clwidth(thick) clpattern(shortdash))	||
			(line s_ability_B2 media_cat, clcolor(green) clwidth(thick) clpattern(shortdash))	||
			(scatter s_ability3 media_cat, msymbol(Oh) msize(small) mcolor(red) sort) 			|| 
			(line s_ability_A3 media_cat, clcolor(red) clwidth(thick) clpattern(longdash))		||
			(line s_ability_B3 media_cat, clcolor(red) clwidth(thick) clpattern(longdash))		||
			(scatter s_ability5 media_cat, msymbol(Oh) msize(small) mcolor(gs4) sort) 			|| 
			(line s_ability_A5 media_cat, clcolor(gs4) clwidth(thin) clpattern(solid))			||
			(line s_ability_B5 media_cat, clcolor(gs4) clwidth(thin) clpattern(solid)), 
			 legend(off)
			 xlabel(-2.5(1)2.5, labsize(medium))
			 ylabel(2(2)10, labsize(medium))
			 title(Panel F: Academic ability, position(11) size(medlarge))
			 ytitle("")
			 xtitle("")
			 xline(0)
			 scheme(s1color)
			 saving("${Results}\Figures\Figure2F.gph",replace);		 
			 
		graph export "${Results}\Figures\Figure2F.png", as(png) replace;
		graph export "${Results}\Figures\Figure2F.eps", as(eps) name("Graph") preview(off) replace;
	
	
	*** Student performance, self-perception
	#delimit;
	
	twoway 	(scatter s_perform4 media_cat, msymbol(Oh) msize(small) mcolor(blue) sort) 			|| 
			(line s_perform_A4 media_cat, clcolor(blue) clwidth(thick) clpattern(solid))		||
			(line s_perform_B4 media_cat, clcolor(blue) clwidth(thick) clpattern(solid))		||
			(scatter s_perform5 media_cat, msymbol(Oh) msize(small) mcolor(gs4) sort) 			|| 
			(line s_perform_A5 media_cat, clcolor(gs4) clwidth(thin) clpattern(solid))			||
			(line s_perform_B5 media_cat, clcolor(gs4) clwidth(thin) clpattern(solid)), 
			 legend(off)
			 xlabel(-2.5(1)2.5, labsize(medium))
			 ylabel(2(2)10, labsize(medium))
			 title(Panel G: Academic performance, position(11) size(medlarge))
			 ytitle(Average rank, size(medsmall))
			 xtitle(Normed transition score, size(medlarge))
			 xline(0)
			 scheme(s1color)
			 saving("${Results}\Figures\Figure2G.gph",replace);		 
			 
		graph export "${Results}\Figures\Figure2G.png", as(png) replace;
		graph export "${Results}\Figures\Figure2G.eps", as(eps) name("Graph") preview(off) replace;
		
		
	*** Student performance, peer reports
	#delimit;
	
	twoway 	(scatter s_perform2 media_cat, msymbol(Oh) msize(small) mcolor(green) sort) 		|| 
			(line s_perform_A2 media_cat, clcolor(green) clwidth(thick) clpattern(shortdash))	||
			(line s_perform_B2 media_cat, clcolor(green) clwidth(thick) clpattern(shortdash))	||
			(scatter s_perform3 media_cat, msymbol(Oh) msize(small) mcolor(red) sort) 			|| 
			(line s_perform_A3 media_cat, clcolor(red) clwidth(thick) clpattern(longdash))		||
			(line s_perform_B3 media_cat, clcolor(red) clwidth(thick) clpattern(longdash))		||
			(scatter s_perform5 media_cat, msymbol(Oh) msize(small) mcolor(gs4) sort) 			|| 
			(line s_perform_A5 media_cat, clcolor(gs4) clwidth(thin) clpattern(solid))			||
			(line s_perform_B5 media_cat, clcolor(gs4) clwidth(thin) clpattern(solid)), 
			 legend(off)
			 xlabel(-2.5(1)2.5, labsize(medium))
			 ylabel(2(2)10, labsize(medium))
			 title(Panel H: Academic performance, position(11) size(medlarge))
			 ytitle("")
			 xtitle(Normed transition score, size(medlarge))
			 xline(0)
			 scheme(s1color)
			 saving("${Results}\Figures\Figure2H.gph",replace);		 
			 
		graph export "${Results}\Figures\Figure2H.png", as(png) replace;
		graph export "${Results}\Figures\Figure2H.eps", as(eps) name("Graph") preview(off) replace;
	
	
	#delimit cr
	
	
	
	****************************************************************************
	****************************************************************************
	**** Figure 2: Combining Panels A to H
	
	* title("Figure 2: Self-perceptions, peer perceptions, and teacher perceptions", pos(6) size(small)) ///

	
	graph combine  	"${Results}\Figures\Figure2A.gph" "${Results}\Figures\Figure2B.gph" 						///
					"${Results}\Figures\Figure2C.gph" "${Results}\Figures\Figure2D.gph" 						///
					"${Results}\Figures\Figure2E.gph" "${Results}\Figures\Figure2F.gph" 						///
					"${Results}\Figures\Figure2G.gph" "${Results}\Figures\Figure2H.gph", 						///
					ysize(6) xsize(4) holes(3 6 9) scheme(s1color) rows(4) ycommon 								///
					graphregion(c(white) margin(10 10 1 1))														///
					imargin(5) saving("${Results}\Figures\Figure2_AllPanels.gph", replace)
				
		graph export "${Results}\Figures\Figure2_AllPanels.png", as(png) replace
		graph export "${Results}\Figures\Figure2_AllPanels.eps", as(eps) name("Graph") preview(off) replace
		
		
		
********************************************************************************
********************************************************************************
*************************** TABLES IN MAIN TEXT ********************************
********************************************************************************
********************************************************************************

	*****************************
	**** Setting up the data ****
	*****************************
	
	use "$pdata\analysis_sample.dta", clear

	** Control variables

		global control_vars				"i.girl_reporting age_child2_reporting i.nation_reporting i.siblings_reporting i.mom_ed_reporting i.alphabet"
		global control_vars_1			"i.girl_reporting age_child2_reporting i.nation_reporting i.siblings_reporting i.alphabet"
		global control_vars_ranked		"i.girl age_child2 i.nation i.siblings i.mom_ed i.alphabet"
		global control_vars_ranking 	"i.girl_reporting age_child2_reporting i.nation_reporting i.siblings_reporting i.mom_ed_reporting i.alphabet_reporting"

		
********************************************************************************
********************************* TABLE 1 **************************************
********************************************************************************

	*********************************
	** No control variables included

		local Table1a "${Results}\Tables\Table1a"
		local Table1b "${Results}\Tables\Table1b"
		
		cap erase "`Table1a'.xls"
		cap erase "`Table1a'.txt"

		cap erase "`Table1b'.xls"
		cap erase "`Table1b'.txt"
		
		foreach var of varlist better_class s_confid s_effort s_ability s_perform s_disrupt {
			* Panel A
			areg `var' above media_std above_media_std if self_report_==1, robust absorb(id_liceu_disc) cluster(id_child2) 
			quietly summ `var' if e(sample)==1
			outreg2 using "${Results}\Tables\Table1a.xls", se bracket coefastr addstat(Mean, r(mean)) bdec(4) sdec(4) excel slow(2000) append
			
			* Panel B
			areg `var' above media_std above_media_std if informants==4, absorb(id_liceu_disc) robust cluster(id_child2) 
			quietly summ `var' if e(sample)==1
			outreg2 using "${Results}\Tables\Table1b.xls", se bracket coefastr addstat(Mean, r(mean)) bdec(4) sdec(4) excel slow(2000) append
			
		}	
		
	**** Absolute measures of student's perceptions (Columns (7) and (8) in Panel A)
			
		local Table1abs "${Results}\Tables\Table1abs"
		
		cap erase "`Table1abs'.xls"
		cap erase "`Table1abs'.txt"
		
		foreach var of varlist rosen_std bullying1c_std {

			areg `var' above media_std above_media_std if self_report_==1, robust absorb(id_liceu_disc) cluster(id_child2) 
			quietly summ `var' if e(sample)==1
			outreg2 using "${Results}\Tables\Table1abs.xls", se bracket coefastr addstat(Mean, r(mean)) bdec(4) sdec(4) excel slow(2000) append
					
		}	

		*better_class QA5 QA10 QA14I1 QA14I2 QA14I3 QA14I4 QA14I5 QA14I6 took_peer_bac bac_peer_raw bac_rank 
	
	
********************************************************************************
********************************* TABLE 2 **************************************
********************************************************************************

	*************************************
	** Regressions exclude self-reports
	
	local Table2a "${Results}\Tables\Table2a"
	local Table2b "${Results}\Tables\Table2b"
	
	cap erase "`Table2a'.xls"
	cap erase "`Table2a'.txt"

	cap erase "`Table2b'.xls"
	cap erase "`Table2b'.txt"
	
	foreach var of varlist better_class s_confid s_effort s_ability s_perform s_disrupt {
		* Panel A
		areg `var' above media_std above_media_std if reporter_higher==1 & self_report_==0, absorb(id_liceu_disc) robust cluster(id_child2) 
		quietly summ `var' if e(sample)==1
		outreg2 using "${Results}\Tables\Table2a.xls", se bracket coefastr addstat(Mean, r(mean)) bdec(4) sdec(4) excel slow(2000) append
		
		* Panel B
		areg `var' above media_std above_media_std if reporter_lower==1 & self_report_==0, absorb(id_liceu_disc) robust cluster(id_child2) 
		quietly summ `var' if e(sample)==1
		outreg2 using "${Results}\Tables\Table2b.xls", se bracket coefastr addstat(Mean, r(mean)) bdec(4) sdec(4) excel slow(2000) append
		
	}	
		
	
********************************************************************************
********************************* TABLE 3 **************************************
********************************************************************************

	*********************************
	** No control variables included
	
	local Table3a "${Results}\Tables\Table3a"
	local Table3b "${Results}\Tables\Table3b"
	
	cap erase "`Table3a'.xls"
	cap erase "`Table3a'.txt"
	
	cap erase "`Table3b'.xls"
	cap erase "`Table3b'.txt"

foreach var of varlist s_confid s_effort s_ability s_perform s_disrupt {
		
		* Panel A
		areg `var' i.informants34, absorb(id_child) robust cluster(id_child2)
		quietly summ `var' if e(sample)==1
		outreg2 using "${Results}\Tables\Table3a.xls", se bracket coefastr addstat(Mean, r(mean)) bdec(4) sdec(4) excel slow(2000) append
		
		* Panel B
		areg `var' i.informants34##c.media_std, absorb(id_child) robust cluster(id_child2) 
		quietly summ `var' if e(sample)==1
		outreg2 using "${Results}\Tables\Table3b.xls", se bracket coefastr addstat(Mean, r(mean)) bdec(4) sdec(4) excel slow(2000) append
				
	}

	
	
************************************************************************************************************************************************************************************************************************************************
************************************************************************************************************************************************************************************************************************************************
************************************************************************************************************************************************************************************************************************************************
************************************************************************************************************************************************************************************************************************************************
************************************************************************************************************************************************************************************************************************************************
************************************************************************************************************************************************************************************************************************************************